package edu.fdzc.learn.service.impl;

import edu.fdzc.learn.entity.HomeSub;
import edu.fdzc.learn.mapper.HomeSubMapper;
import edu.fdzc.learn.service.HomeSubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class HomeSubServiceImpl implements HomeSubService {

    @Autowired
    private HomeSubMapper homeSubMapper;

    @Override
    public void insert(HomeSub homeSub) {
        homeSub.setStatus(1);//已提交
        homeSub.setSubmittedAt(LocalDateTime.now());
        int id = homeSub.getStudentId();
        HomeSub homeSub1 = homeSubMapper.findBySId(id);
        if(homeSub1 != null) {
            homeSub1.setSubmittedAt(homeSub.getSubmittedAt());
            homeSub1.setContent(homeSub.getContent());
            homeSub1.setAttachmentUrl(homeSub.getAttachmentUrl());
            homeSubMapper.update(homeSub1);
            return ;
        }
        homeSubMapper.insert(homeSub);
    }

    @Override
    public void update(HomeSub homeSub) {
        homeSubMapper.update(homeSub);
    }

    @Override
    public HomeSub findBySId(int studentId) {
        HomeSub homeSub = homeSubMapper.findBySId(studentId);
        return homeSub;
    }

    @Override
    public List<HomeSub> findAll(int homeworkId) {
        List<HomeSub> list = homeSubMapper.findAll(homeworkId);
        return list;
    }
}
